# ReKep Real Camera Dependencies Installation Script for Windows
# PowerShell Script

Write-Host "============================================" -ForegroundColor Cyan
Write-Host "ReKep Real Camera Setup (Windows)" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""

# Check if running as administrator
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)

if (-not $isAdmin) {
    Write-Host "Warning: Not running as administrator" -ForegroundColor Yellow
    Write-Host "Some operations may require administrator privileges" -ForegroundColor Yellow
    Write-Host ""
}

# Step 1: Check RealSense SDK
Write-Host "[Step 1] Checking Intel RealSense SDK..." -ForegroundColor Cyan
try {
    $realsensePath = Get-Command realsense-viewer -ErrorAction SilentlyContinue
    if ($realsensePath) {
        Write-Host "✓ RealSense SDK is installed" -ForegroundColor Green
        Write-Host "  Path: $($realsensePath.Source)" -ForegroundColor Gray
    } else {
        Write-Host "✗ RealSense SDK not found" -ForegroundColor Red
        Write-Host "  Please download and install from:" -ForegroundColor Yellow
        Write-Host "  https://github.com/IntelRealSense/librealsense/releases" -ForegroundColor Yellow
        Write-Host ""
        Write-Host "  After installation, restart this script" -ForegroundColor Yellow
    }
} catch {
    Write-Host "✗ RealSense SDK check failed" -ForegroundColor Red
    Write-Host "  Error: $_" -ForegroundColor Red
}

Write-Host ""

# Step 2: Install Python packages
Write-Host "[Step 2] Installing Python packages..." -ForegroundColor Cyan
Write-Host ""

# Check Python
try {
    $pythonVersion = python --version
    Write-Host "Python version: $pythonVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ Python not found" -ForegroundColor Red
    Write-Host "  Please install Python 3.8+ from: https://www.python.org/" -ForegroundColor Yellow
    exit 1
}

# Install packages
Write-Host "Installing pyrealsense2..." -ForegroundColor Cyan
pip install pyrealsense2

if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ pyrealsense2 installed successfully" -ForegroundColor Green
} else {
    Write-Host "✗ Failed to install pyrealsense2" -ForegroundColor Red
}

Write-Host ""

# Step 3: Optional - SAM
Write-Host "[Step 3] Optional: Install Segment Anything Model (SAM)" -ForegroundColor Cyan
$installSAM = Read-Host "Install SAM? (y/N)"

if ($installSAM -eq 'y' -or $installSAM -eq 'Y') {
    Write-Host "Installing SAM..." -ForegroundColor Cyan
    pip install git+https://github.com/facebookresearch/segment-anything.git
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ SAM installed successfully" -ForegroundColor Green
        Write-Host ""
        Write-Host "Download SAM checkpoint:" -ForegroundColor Yellow
        Write-Host "  wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth" -ForegroundColor Yellow
        Write-Host "  Or download manually from: https://dl.fbaipublicfiles.com/segment_anything/" -ForegroundColor Yellow
    } else {
        Write-Host "✗ Failed to install SAM" -ForegroundColor Red
    }
}

Write-Host ""

# Step 4: Verify installation
Write-Host "[Step 4] Verifying installation..." -ForegroundColor Cyan
python -c "import pyrealsense2 as rs; print('✓ pyrealsense2 works')" 2>$null

if ($LASTEXITCODE -eq 0) {
    Write-Host "✓ Python packages verified" -ForegroundColor Green
} else {
    Write-Host "✗ Verification failed" -ForegroundColor Red
}

Write-Host ""

# Step 5: Check for camera
Write-Host "[Step 5] Checking for connected cameras..." -ForegroundColor Cyan

try {
    python -c "import pyrealsense2 as rs; import sys; ctx = rs.context(); devices = ctx.query_devices(); sys.exit(0 if len(devices) > 0 else 1)" 2>$null
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Camera detected" -ForegroundColor Green
    } else {
        Write-Host "⚠ No camera detected" -ForegroundColor Yellow
        Write-Host "  Connect your RealSense camera and try again" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠ Could not check for camera" -ForegroundColor Yellow
    Write-Host "  Make sure camera is connected" -ForegroundColor Yellow
}

Write-Host ""

# Summary
Write-Host "============================================" -ForegroundColor Cyan
Write-Host "Setup Summary" -ForegroundColor Cyan
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""

Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host "1. Connect your RealSense camera (if not already connected)" -ForegroundColor White
Write-Host "2. Run: python DIY/realsense_camera.py" -ForegroundColor White
Write-Host "3. Run: python DIY/test_with_real_camera.py" -ForegroundColor White
Write-Host ""

Write-Host "For detailed instructions, see:" -ForegroundColor Cyan
Write-Host "  DIY/README_复现指引.md" -ForegroundColor White
Write-Host "  DIY/复现总览.md" -ForegroundColor White
Write-Host ""

Write-Host "Setup completed!" -ForegroundColor Green
Write-Host ""

